using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using System.Data.SqlClient;
using NCS.Module.LinkSite.Library.Data;

namespace NCS.Module.LinkSite.Library.DataAccess
{
    public class Live_SupporDB
    {
        public static DataTable GetAll()
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataTable getAllByActive(int portalid, string lang)
        {
            DataTable retVal = null;
            SqlConnection con = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand cmd = new SqlCommand("getAllByActive", con);
            cmd.Parameters.AddWithValue("@PortalID", portalid);
            cmd.Parameters.AddWithValue("@Lang", lang);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(retVal);
            }
            finally {
                con.Close();
            }
            return retVal;
        }
        public static DataTable GetAllByPortal(int PortalId, string Language)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_GetAllbyPortal", dbConn);
            dbCmd.Parameters.Add("@PortalID", PortalId);
            dbCmd.Parameters.Add("@Laguage", Language);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static void Delete(int _iD)
        {
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add("@ID", _iD);
            try
            {
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
            }
            finally
            {
                dbConn.Close();
            }
        }
        public static int Insert(Live_SupporInfo _live_SupporInfo)
        {
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add("@Name", _live_SupporInfo.Name);
            dbCmd.Parameters.Add("@Description", _live_SupporInfo.Description);
            dbCmd.Parameters.Add("@PortalID", _live_SupporInfo.PortalID);
            dbCmd.Parameters.Add("@Laguage", _live_SupporInfo.Laguage);
            dbCmd.Parameters.Add("@Post", _live_SupporInfo.Post);
            dbCmd.Parameters.Add("@Active", _live_SupporInfo.Active);
            dbCmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            try
            {
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
                return (int)dbCmd.Parameters["@RETURN_VALUE"].Value;
            }
            finally
            {
                dbConn.Close();
            }
        }

        public static void Update(Live_SupporInfo _live_SupporInfo)
        {
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add("@ID", _live_SupporInfo.ID);
            dbCmd.Parameters.Add("@Name", _live_SupporInfo.Name);
            dbCmd.Parameters.Add("@Description", _live_SupporInfo.Description);
            dbCmd.Parameters.Add("@PortalID", _live_SupporInfo.PortalID);
            dbCmd.Parameters.Add("@Laguage", _live_SupporInfo.Laguage);
            dbCmd.Parameters.Add("@Post", _live_SupporInfo.Post);
            dbCmd.Parameters.Add("@Active", _live_SupporInfo.Active);
            try
            {
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
            }
            finally
            {
                dbConn.Close();
            }
        }

        public static Live_SupporInfo GetInfo(int _iD)
        {
            Live_SupporInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("Live_Support_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add("@ID", _iD);
            try
            {
                dbConn.Open();
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new Live_SupporInfo();
                    retVal.ID = Convert.ToInt32(dr["ID"]);
                    retVal.Name = Convert.ToString(dr["Name"]);
                    retVal.Description = Convert.ToString(dr["Description"]);
                    retVal.PortalID = Convert.ToInt32(dr["PortalID"]);
                    retVal.Laguage = Convert.ToString(dr["Laguage"]);
                    retVal.Post = Convert.ToInt32(dr["Post"]);
                    retVal.Active = Convert.ToBoolean(dr["Active"]);
                }
                if (dr != null) dr.Close();
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

    }
}
